import { authRefreshToken } from "@/apis/yz";
import { useUser } from "@/stores/useUser";

export const initUser = () => {
	return new Promise(async (resolve, reject) => {
		try {
			const userStore = useUser();
			const token = localStorage.getItem("token");
			if (token) {
				const { data } = await authRefreshToken({ token }).catch(() => {
					throw new Error();
				});
				userStore.setState({ token: data.token });
				await userStore.syncUserInfo();
				resolve(null);
			} else throw new Error();
		} catch (error) {
			localStorage.removeItem("token");
			reject();
		}
	});
};
